package dao.impl;

import dao.CartDao;
import entity.Cart;
import entity.Product;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.JDBCUtils;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author: xxf
 * @CreateDate: 2020/10/6 18:39
 */
public class CartDaoImpl implements CartDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 判断购物车是否存在
     *
     * @param pid 商品id
     * @param uid 用户id
     * @return 返回购物车对象
     */
    @Override
    public Cart hasCart(int pid, int uid) throws InvocationTargetException, IllegalAccessException {
//        String sql ="select c_id cid,u_id uid,c_count ccount,c_num cnum from cart where p_id =? and u_id =?";
//        Cart cart = template.queryForObject(sql, new BeanPropertyRowMapper<Cart>(Cart.class), pid, uid);
        String sql = "select p.p_id pid,p.t_id tid,p.p_name pname,p.p_image pimage," +
                "p.p_price pprice,p.p_state pstate,p.p_info pinfo," +
                "c.c_id cid,c.u_id uid,c.c_count ccount,c.c_num cnum from " +
                "product p join cart c on p.p_id =c.p_id where c.p_id =? and c.u_id =?";
        Map<String, Object> map = null;
        try {
            map = template.queryForMap(sql, pid, uid);
        } catch (DataAccessException e) {
            e.printStackTrace();
            System.out.println("hasCart从数据库获取的map为空");
            if (map == null) {
                return null;
            }
        }
        Cart cart = new Cart();
        Product product = new Product();

        BeanUtils.populate(cart, map);
        BeanUtils.populate(product, map);

        cart.setProduct(product);

        return cart;
    }

    /**
     * 更新购物车中的商品数量
     *
     * @param cart
     */
    @Override
    public void updateCart(Cart cart) {
        String sql = "update cart set c_num=?, c_count = ? where c_id = ?";
        template.update(sql, cart.getCnum(), cart.getCcount(), cart.getCid());
    }

    /**
     * 创建一个购物车对象给数据库插入数据
     *
     * @param cart
     */
    @Override
    public void insertCart(Cart cart) {
        String sql = "insert into cart(p_id,u_id,c_count,c_num) values(?,?,?,?)";
        template.update(sql, cart.getPid(), cart.getUid(), cart.getCcount(), cart.getCnum());
    }

    /**
     * 查询购物车商品，需要联合查询
     * @param uid
     * @return
     */
    @Override
    public List<Cart> findCartByUid(int uid) throws InvocationTargetException, IllegalAccessException {
        String sql = "select p.p_id pid,p.t_id tid,p.p_name pname,p.p_image pimage," +
                "p.p_price pprice,p.p_state pstate,p.p_info pinfo," +
                "c.c_id cid,c.u_id uid,c.c_count ccount,c.c_num cnum from " +
                "product p join cart c on p.p_id =c.p_id where c.u_id =?";
        List<Map<String, Object>> list = template.queryForList(sql, uid);

        //返回list为空直接返回
        if (list == null) {
            return null;
        }
        //在内存中创建一个装cart对象的容器
        List<Cart> carts = new ArrayList<>();

        //遍历查询返回的list集合
        for (Map<String, Object> map : list) {
            //返回不为空，装对象
            Cart cart = new Cart();
            Product product = new Product();

            BeanUtils.populate(cart,map);
            BeanUtils.populate(product,map);

            cart.setProduct(product);

            //装进集合
            carts.add(cart);

        }
        return carts;
    }

    /**
     * 根据购物车id删除购物车
     * @param cid
     */
    @Override
    public void deleteCartByCid(int cid) {
        String sql = "delete from cart where c_id =?";
        template.update(sql, cid);
    }

    /**
     * 修改购物车
     * @param cid 购物车id
     * @param cnum 购物车数量
     * @param count 购物车小计
     */
    @Override
    public void updateCartByCid(int cid, int cnum, BigDecimal count) {
        String sql = "update cart set c_num = ?, c_count = ? where c_id = ?;";
        template.update(sql, cnum, count, cid);
    }

    /**
     * 根据用户id清空购物车所有商品
     * @param uid
     */
    @Override
    public void deleteCartByUid(int uid) {
        String sql = "delete from cart where u_id = ?";
        template.update(sql, uid);
    }
}
